import { Injectable } from '@angular/core'
import { Subject, BehaviorSubject, Observable } from 'rxjs'
import { ObservableStore } from '@shared/classes/ObservableStoreClass'
import { ETabScrollStatus } from '@shared/enums'

@Injectable({
  providedIn: 'root',
})
export class PageService {
  /* 详情页基本信息是否进入编辑状态 */

  /* 新增页提交成功，是否解锁路由守卫 */
  private _isGuardAlive$: BehaviorSubject<boolean>
  public isGuardAlive$: Observable<boolean>

  // private isBaseEdit: boolean
  private _isBaseEdit$: BehaviorSubject<boolean>
  public isBaseEdit$: Observable<boolean>

  /* 提交按钮是否开启摇晃效果 */
  private _isShake$: BehaviorSubject<boolean>
  public isShake$: Observable<boolean>

  /* 检查是否自动拉去tab的状态 */
  private _tabScrollStatus$: BehaviorSubject<ETabScrollStatus>
  public tabScrollStatus$: Observable<ETabScrollStatus>

  /* Edit tab info */
  private _isTabEdit$: BehaviorSubject<boolean>
  public isTabEdit$: Observable<boolean>

  /** 是否编辑渠道 */
  private _isChannelAdd$: BehaviorSubject<boolean>
  public isChannelAdd$: Observable<boolean>

  /** 是否编辑交易信息 */
  private _isTradeEdit$: BehaviorSubject<boolean>
  public isTradeEdit$: Observable<boolean>

  /** 是否在任务中展示门店信息 */
  private _isStoreDisplayInTask$: BehaviorSubject<boolean>
  public isStoreDisplayInTask$: Observable<boolean>

  /** 门店是否已经进审核 */
  private _isStoreLocked$: BehaviorSubject<boolean>
  public isStoreLocked$: Observable<boolean>

  /** 门店是否需要审核 */
  private _isStoreNeedAudit$: BehaviorSubject<boolean>
  public isStoreNeedAudit$: Observable<boolean>

  /** 门店是否有修改 */
  private _isStoreBeenModified$: BehaviorSubject<boolean>
  public isStoreBeenModified$: Observable<boolean>

  constructor() {
    this._isBaseEdit$ = new BehaviorSubject(false)
    this.isBaseEdit$ = this._isBaseEdit$.asObservable()

    this._isGuardAlive$ = new BehaviorSubject(true)
    this.isGuardAlive$ = this._isGuardAlive$.asObservable()

    this._isShake$ = new BehaviorSubject(false)
    this.isShake$ = this._isShake$.asObservable()

    this._tabScrollStatus$ = new BehaviorSubject(ETabScrollStatus.InitialLoad)
    this.tabScrollStatus$ = this._tabScrollStatus$.asObservable()

    this._isTabEdit$ = new BehaviorSubject(false)
    this.isTabEdit$ = this._isTabEdit$.asObservable()

    this._isChannelAdd$ = new BehaviorSubject(false)
    this.isChannelAdd$ = this._isChannelAdd$.asObservable()

    this._isTradeEdit$ = new BehaviorSubject(false)
    this.isTradeEdit$ = this._isTradeEdit$.asObservable()

    this._isStoreDisplayInTask$ = new BehaviorSubject(false)
    this.isStoreDisplayInTask$ = this._isStoreDisplayInTask$.asObservable()

    this._isStoreLocked$ = new BehaviorSubject(false)
    this.isStoreLocked$ = this._isStoreLocked$.asObservable()

    this._isStoreNeedAudit$ = new BehaviorSubject(true)
    this.isStoreNeedAudit$ = this._isStoreNeedAudit$.asObservable()

    this._isStoreBeenModified$ = new BehaviorSubject(false)
    this.isStoreBeenModified$ = this._isStoreBeenModified$.asObservable()
  }

  get isBaseEdit(): boolean {
    return this._isBaseEdit$.getValue()
  }

  public triggerBaseEdit(isEdit: boolean = false) {
    this._isBaseEdit$.next(isEdit)
  }

  get isGuardAlive(): boolean {
    return this._isGuardAlive$.getValue()
  }

  public triggerGuardAlive(ifLive: boolean): void {
    this._isGuardAlive$.next(ifLive)
  }

  get isShake(): boolean {
    return this._isShake$.getValue()
  }

  public triggerShake(isShake: boolean): void {
    this._isShake$.next(isShake)
  }

  get tabStatus(): ETabScrollStatus {
    return this._tabScrollStatus$.getValue()
  }

  public setTabScrollStatus(status: number) {
    if (this.tabStatus === ETabScrollStatus.InitialLoad) {
      this._tabScrollStatus$.next(ETabScrollStatus.Stop)
    } else {
      this._tabScrollStatus$.next(status)
    }
  }

  get isTabEdit(): boolean {
    return this._isTabEdit$.getValue()
  }

  public triggerTabEdit(isEdit: boolean = false) {
    this._isTabEdit$.next(isEdit)
  }

  get isChannelAdd(): boolean {
    return this._isChannelAdd$.getValue()
  }

  public setChannelAdd(isAdd: boolean = false) {
    this._isChannelAdd$.next(isAdd)
  }

  get isTradeEdit(): boolean {
    return this._isTradeEdit$.getValue()
  }

  public setTradeEdit(isEdit: boolean = false) {
    this._isTradeEdit$.next(isEdit)
  }

  get isStoreDisplayInTask(): boolean {
    return this._isStoreDisplayInTask$.getValue()
  }

  public setStoreDisplayInTask(isDisplay: boolean = false) {
    this._isStoreDisplayInTask$.next(isDisplay)
  }

  get isStoreLocked(): boolean {
    return this._isStoreLocked$.getValue()
  }

  public setStoreLocked(isLocked: boolean = false) {
    this._isStoreLocked$.next(isLocked)
  }

  get isStoreNeedAudit(): boolean {
    return this._isStoreNeedAudit$.getValue()
  }

  public setStoreNeedAudit(isNeed: boolean) {
    this._isStoreNeedAudit$.next(isNeed)
  }

  get isStoreBeenModified(): boolean {
    return this._isStoreBeenModified$.getValue()
  }

  public setStoreBeenModified(isModified: boolean) {
    this._isStoreBeenModified$.next(isModified)
  }
}
